package com.hyx.java.interceptor;

import com.hyx.java.base.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@ControllerAdvice
public class GlobalControllerInterceptor {
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Object processException(HttpServletRequest request, HttpServletResponse response, Exception exception) {
        log.error(String.format("【%s】 error %s", request.getRequestURI(), exception.getMessage()), exception);
        return BaseResult.fail("000", exception.getMessage());
    }
}
